import {
    getmeinfo
} from '@/api/upms/user'
import util from '@/libs/util.js'
export default {
    namespaced: true,
    state: {
        // 用户信息
        info: {},
        // 业务信息
        business: []
    },
    getters:{
        userinfo: state=>state.info
    },
    actions: {
        /**
         * @description 设置用户数据
         * @param {Object} state vuex state
         * @param {*} info info
         */
        set({
            state,
            dispatch
        }, info) {
            return new Promise(async resolve => {
                // store 赋值
                state.info = info
                // 持久化
                await dispatch('d2admin/db/set', {
                    dbName: 'sys',
                    path: 'user.info',
                    value: info,
                    user: true
                }, {
                    root: true
                })
                // end
                resolve()
            })
        },
        get({
            state,
            dispatch
        }) {
            return new Promise(async resolve => {

                getmeinfo(util.cookies.get('token')).then(data => {
                    // store 赋值
                    state.info = data
                })
                // 持久化
                await dispatch('d2admin/db/set', {
                    dbName: 'sys',
                    path: 'user.info',
                    value: state.info,
                    user: true
                }, {
                    root: true
                })

                // end
                resolve()
            })
        },
        /**
         * @description 从数据库取用户数据
         * @param {Object} state vuex state
         */
        load({
            state,
            dispatch
        }) {
            return new Promise(async resolve => {
                // store 赋值
                state.info = await dispatch('d2admin/db/get', {
                    dbName: 'sys',
                    path: 'user.info',
                    defaultValue: {},
                    user: true
                }, {
                    root: true
                })
                // end
                resolve()
            })
        },


        // ----------业务信息----------
        /**
         * @description 设置用户数据
         * @param {Object} state vuex state
         * @param {*} info info
         */
        setBusiness({
            state,
            dispatch
        }, info) {
            return new Promise(async resolve => {
                // store 赋值
                state.business = info
                // 持久化
                await dispatch('d2admin/db/set', {
                    dbName: 'sys',
                    path: 'user.business',
                    value: info,
                    user: true
                }, {
                    root: true
                })
                // end
                resolve()
            })
        },
        /**
         * @description 从数据库取用户数据
         * @param {Object} state vuex state
         */
        loadBusiness({
            state,
            dispatch
        }) {
            return new Promise(async resolve => {
                // store 赋值
                state.business = await dispatch('d2admin/db/get', {
                    dbName: 'sys',
                    path: 'user.business',
                    defaultValue: [],
                    user: true
                }, {
                    root: true
                })

                console.log("数据库信息", JSON.stringify(state.business))
                // end
                resolve()
            })
        }
    }
}